
#include <asm.h>

.global C_SYMBOL(GradientFill)

C_SYMBOL(GradientFill):

width = -0x28
gradientTable = -0x24
incy_16 = -0x20
y0 = -0x1C
x0 = -0x18
nDstPitch= -0x14
pDst= -0x10

    STMFD   SP!, {R4-R12,LR}
    SUB     SP, SP, #0x4C
Start:

    LDR     R12, [R0,#0x0]           //pDst
    STR     R12, [SP,#0x4C+pDst]

    LDR     R11, [R0,#0x4]           //nPitch
    STR     R11, [SP,#0x4C+nDstPitch]

    LDR     R10, [R0,#0x8]

    LDR     R9,  [R0,#0xC]
    STR     R9,  [SP,#0x4C+incy_16]

    LDR     R7, [R0,#0x10]          //x0

    LDR     R8, [R0,#0x14]          //y0
    STR     R8, [SP,#0x4C+y0]

    LDR     R6, [R0,#0x18]
    STR     R6, [SP,#0x4C+gradientTable]

    MUL     R11, R7, R10             //R11 = gx0_16
    RSB     R11, R11, #0

    LDR     R7, [R0,#0x1C]          //width
    STR     R7, [SP,#0x4C+width]

    LDR     R1, =0x00000000
    TMCRR   wR14 , R1, R1
    LDR     R1, = 0x00FF00FF
    TMCRR   wR15, R1, R1

    LDR     R0, [SP,#0x4C+pDst]
    LDR     R1, [SP,#0x4C+nDstPitch]
    LDR     R8, [SP,#0x4C+y0]
    LDR     R7, [SP,#0x4C+width]
    MLA     LR, R1, R8, R0
    MOV     R5, R11                 //R5 = gx_16

LOOPX:
    MOV     R0, R5, ASR #16
    ADD     R5, R5, R10
    MOV     R1, R5, ASR #16
    MOV     R1, R1, LSL #16
    ORR     R0, R0, R1
    ADD     R5, R5, R10
    MOV     R2, R5, ASR #16
    ADD     R5, R5, R10
    MOV     R3, R5, ASR #16
    MOV     R3, R3, LSL #16
    ORR     R2, R2, R3
    TMCRR   wR0, R0, R2
    WMAXSH  wR0, wR0, wR14
    SUBS    R7, R7, #4
    WMINSH  wR0, wR0, wR15
    ADD     R5, R5, R10
    TEXTRMUH R4, wR0, #0
    TEXTRMUH R8, wR0, #1
    ADD     R12, R6, R4, LSL #2
    LDR     R0, [R12]
    ADD     R12, R6, R8, LSL #2
    LDR     R1, [R12]

    TEXTRMUH R4, wR0, #2
    TEXTRMUH R8, wR0, #3
    ADD     R12, R6, R4, LSL #2
    LDR     R2, [R12]
    ADD     R12, R6, R8, LSL #2
    LDR     R3, [R12]

    STR     R0, [LR], #4
    STR     R1, [LR], #4
    STR     R2, [LR], #4
    STR     R3, [LR], #4


    BGT     LOOPX

    LDR     R0, [SP,#0x4C+pDst]
    LDR     R1, [SP,#0x4C+nDstPitch]
    LDR     R8, [SP,#0x4C+y0]
    LDR     R7, [SP,#0x4C+width]
    ADD     R11, R11, R9
    SUBS    R8, R8, #1
    MLA     LR, R1, R8, R0
    MOV     R5, R11                 //R5 = gx_16

    STR     R8, [SP,#0x4C+y0]

    BGE     LOOPX

End:
    ADD     SP, SP, #0x4C
    LDMFD   SP!, {R4-R12,PC}

